/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upgmadendro.utils;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.StringTokenizer;
import upgmadendro.population.Population;

/**
 *
 * @author Shadev
 */
public class MainParser {
    private BufferedReader input=null;
    private StringTokenizer stok=null;

    public MainParser(String location) throws FileNotFoundException{
        ClassLoader cl = this.getClass().getClassLoader();
            input =  new BufferedReader(new InputStreamReader(cl.getResourceAsStream(location)));
    }
    public  ArrayList<Population> getPopulations() throws FileNotFoundException, IOException{
        String line=input.readLine();
        int[] constrains = getConstrains(line);
        ArrayList<Population> res = new ArrayList<Population>();
        BitSet temp = null;
        Population p = null;

        for(int i=0;i<constrains[0];i++){
            line=input.readLine();
            stok=new StringTokenizer(line);
            p = new Population(constrains[1]);
            temp=p.getGenes();
            p.setName(stok.nextToken());
            for(int j=0;j<constrains[1];j++){
                if(Integer.parseInt(stok.nextToken())==1)
                    temp.set(j);
            }
            res.add(p);
        }
        return res;
    }

    private int[] getConstrains(String firstLine) throws IOException{
        stok=new StringTokenizer(firstLine);
        if(stok.countTokens()!=2){
            throw new IOException("Bad file format");
        }
        int[] res = new int[2];
        res[0]=Integer.parseInt(stok.nextToken());
        res[1]=Integer.parseInt(stok.nextToken());
        return res;
    }
}
